<template>
<div>
    <el-dialog title="农场信息" :visible.sync="dialogFormVisible">
        <el-form :model="form">
            <el-form-item label="农场名称" :label-width="formLabelWidth">
                <el-input v-model="form.name" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="农场面积" :label-width="formLabelWidth">
                <el-input v-model="form.area" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="农场地址" :label-width="formLabelWidth">
                <el-input v-model="form.address" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="种植作物" :label-width="formLabelWidth">
                <el-input v-model="form.zuowu" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="负责员工" :label-width="formLabelWidth">
                <el-select v-model="form.fuze" filterable placeholder="请选择">
                    <el-option
                        v-for="item in options"
                        :key="item.name"
                        :label="item.name"
                        :value="item.name"
                    >
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="save">确 定</el-button>
        </div>
    </el-dialog>
    <el-card>
        <el-row>
            <el-col :span='8'>农场名称：<el-input v-model='input' style='width: 80%'></el-input></el-col>
            <el-col :span='8'>&nbsp;</el-col>
            <el-col :span='8'><el-button @click='search'>检索</el-button>
                                <el-button @click='dialogFormVisible=true'>添加农场</el-button>
            </el-col>
        </el-row>
        <br>
        <br>
        <el-table
            :data="tableData"
            border
            style="width: 100%">
            <el-table-column
                fixed
                prop="name"
                label="农场名称"
                width="150">
            </el-table-column>
            <el-table-column
                prop="area"
                label="农场面积"
                width="120">
            </el-table-column>
            <el-table-column
                prop="adreess"
                label="农场地址"
                width="120">
            </el-table-column>
            <el-table-column
                prop="zuowu"
                label="种植作物"
                width="120">
            </el-table-column>
            <el-table-column
                prop="fuze"
                label="负责员工"
                width="300">
            </el-table-column>
            <el-table-column
                prop="fuzephone"
                label="负责人手机号"
                width="120">
            </el-table-column>
            <el-table-column
                fixed="right"
                label="操作"
                width="100">
                <template slot-scope="scope">
                    <el-button @click="handleClick(scope.row)" type="text" size="small">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <br><br>
        <el-pagination
            background
            layout="prev, pager, next"
            @current-change="handleCurrentChange"
            :current-page='current'
            :total="total">
        </el-pagination>
    </el-card>
</div>
</template>

<script>
export default {
    created() {
        this.$http.get("/v1/farm/list").then(res=>{
            this.tableData = res.records
            this.current = res.current
            this.total = res.total
        })

        this.$http.get("/v1/user/list1").then(res=>{
            this.options = res
        })
    },
    methods: {
        save(){
            console.log(this.form)
            this.$http.post("/v1/farm/save",this.form).then(res=>{
                this.dialogFormVisible = false
                this.form = {}
                this.search()
                alert("添加成功")
            })
        },
        handleClick(row) {

            this.$http.get("/v1/farm/delete",{
                params:{
                    id:row.id
                }
            }).then(res=>{
                this.search()
               alert("删除成功")
            })
        },
        handleCurrentChange(val) {
            if (this.input === ''){
                this.input = null
            }
            this.$http.get("/v1/farm/list",{
                params:{
                    name:this.input,
                    page:val
                }
            }).then(res=>{
                this.tableData = res.records
                this.current = res.current
                this.total = res.total
            })
        },
        search(){
            if (this.input === ''){
                this.input = null
            }
            this.$http.get("/v1/farm/list",{
                params:{
                    name:this.input
                }
            }).then(res=>{
                this.tableData = res.records
                this.current = 1
                this.total = res.total
            })
        }
    },

    data() {
        return {
            current:1,
            total:10,
            input:null,
            tableData: [],
            dialogFormVisible: false,
            form: {},
            formLabelWidth: '120px',
            options: [],
            value: ''
        }
    }
}
</script>

<style scoped>

</style>
